$(function() {           
    var i = 1; 
    
    $('#add_movie_row').live('click', function() {
        var tempName = "movieRow" + i;
        $("#movies_table").append($("\\n\
        <tr id='movie_row'>\n\
            <td></td>\n\
            <td><input type='text' name=" + tempName + 'Title' + " size='30' value='' onblur='fixInput(this)'></td>\n\
            <td><input type='text' name=" + tempName + 'Length' + " size='10' value='00:00:00' onblur='fixLength(this)'></td>\n\
            <td><input type='text' name=" + tempName + 'Genre' + " placeholder='e.g. (comedy)' onblur='fixInput(this)' </td> \n\
            <td><input type='checkbox'  name=" + tempName + 'Language1' +"  value='SK' class='custom-class1 custom-class2'>SK\n\
                <input type='checkbox'  name=" + tempName + 'Language2' +"  value='CZ' class='custom-class1 custom-class2'>CZ\n\
                <input type='checkbox'  name=" + tempName + 'Language3' +"  value='EN' class='custom-class1 custom-class2'>EN\n\
                <input type='checkbox'  name=" + tempName + 'Language4' +"  value='DE' class='custom-class1 custom-class2'>DE\n\
            </td>\n\
            <td><input type='text' name=" + tempName + 'Format' +" size='5' placeholder='Format'></td>\n\
            <td ><input class='input-width' type='text'  name=" + tempName + 'Note' +" size='10' placeholder='Note' ></td>\n\
            <td><a href='#' id='remove'>Remove</a></td></tr>"));                
        i++;                
        return false;       
    });   
  
    
    $('#remove').live('click', function() {
        if( i > 2 ) {
            $(this).parents('tr').remove();
            i--;
        }
        return false;
    });
    
    $('.show-button').live('click',function(){  
        
        $(this).siblings().toggle("slow");        
    });   
    
});

function fixInput(input) {
    
    if (input.value == "" || input.value == null || 
        input.value == "Fix title" || input.value=="00:00:00") {
        isFilled(input);
        input.value = "Unknown";
        
    } else isFilled(input);
}

function isFilled(fld) {
    
    if (fld.value == "" || fld.value == null) {
        fld.style.backgroundColor = '#fba';
    } else {
        fld.style.backgroundColor = '#bfa';
    }
}

function fixLength(fld) 
{     
    var isValid = /^([0-9]?[0-9]|9[0-9]):([0-5][0-9])(:[0-5][0-9])?$/.test(fld.value);

    if (isValid) {
        fld.style.backgroundColor = '#bfa';
    } else {
        fld.value="00:00:00";
        fld.style.backgroundColor = '#fba';
    }
    return isValid;
    
}

function verificate(form) {
    if(form.name.value == "" || form.name.value == null){
        isFilled(form.name);
        alert("Please insert medium name.");
        return false;
    }
    return true;
}


function validateMovie(form) {
    if(form.movieName.value == "" || form.movieName.value == null){
        isFilled(form.movieName);
        alert("Please insert movie name.");
        return false;
    }
    return true;
} 

function validateName(form) {
    if(form.fname.value == "" || form.fname.value == null){
        isFilled(form.fname);  
        alert('Please input file name');
        return false;
    }      
    return true;
}  
            
function checkFile(f){
    f = f.elements;
    if(/.*\.(ods)$/.test(f['ipath'].value.toLowerCase()))
        return true;
    alert('Please Upload Ods File Only.');
    f['ipath'].focus();
    return false;
}



